package com.art.museumbackend.mapper;

import com.art.museumbackend.entity.Admin;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yezi
 * @since 2024-12-06
 */
@Mapper
public interface AdminMapper extends BaseMapper<Admin> {
    @Select("select * from admin where username=#{username}")
    Admin findByUsername(String username);

    @Insert("insert into admin(username, password, create_time, update_time, login_fail_count, lock_status, password_update_time) " +
            "values(#{username}, #{password}, now(), now(), 0, false, now())")
    void add(String username, String password);

    @Update("update admin set nickname=#{nickname},email=#{email},phone=#{phone},update_time=#{updateTime} where id=#{id}")
    void updateInfo(Admin admin);

    @Update("update admin set user_pic=#{avatarUrl},update_time=now() where id=#{id}")
    void updateAvatar(String avatarUrl,Integer id);

    @Update("update admin set password=#{md5String}, update_time=now(), password_update_time=now() where id=#{id}")
    void updatePwd(String md5String, Integer id);

    @Update("update admin set login_fail_count = #{failCount}, lock_status = #{lockStatus}, lock_end_time = #{lockEndTime}, update_time = now() where id = #{id}")
    void updateLoginFailInfo(Integer id, Integer failCount, Boolean lockStatus, LocalDateTime lockEndTime);

    @Update("update admin set login_fail_count = 0, lock_status = false, lock_end_time = null, last_activity_time = now(), update_time = now() where id = #{id}")
    void resetLoginFailInfo(Integer id);

    @Update("update admin set last_activity_time = now() where id = #{id}")
    void updateLastActivityTime(Integer id);
}
